package com.zmn.oms.services.impl.tag;

import com.zmn.common.utils.date.DateUtil;
import com.zmn.oms.common.annotation.OrderWorkChangeSync;
import com.zmn.oms.model.dto.tag.OrderTagDTO;
import com.zmn.oms.model.dto.tag.OrderTagDelDTO;
import com.zmn.oms.model.entity.tag.OrderTag;
import com.zmn.oms.persistence.interfaces.tag.OrderTagDao;
import com.zmn.oms.services.interfaces.tag.OrderTagService;
import io.shardingsphere.api.HintManager;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * 类描述：
 *
 * @author wangxiaokun
 * @since 2021/06/24 16:38
 */
@Service
public class OrderTagServiceImpl implements OrderTagService {

    @Resource
    private OrderTagDao orderTagDao;

    @Override
    public Integer countOrderTagByOrderIdAndTagIdSrcMaster(Long orderId, Integer tagId) {
        try (HintManager hintManager = HintManager.getInstance()) {
            hintManager.setMasterRouteOnly();
            return orderTagDao.countOrderTagByOrderIdAndTagId(orderId, tagId);
        }
    }

    @Override
    public Integer countOrderTagByOrderIdAndTagId(Long orderId, Integer tagId) {
        return orderTagDao.countOrderTagByOrderIdAndTagId(orderId, tagId);
    }

    @Override
    public List<OrderTag> listOrderTagByOrderIdAndTagId(Long orderId, Integer tagId) {
        return orderTagDao.listOrderTagByOrderIdAndTagId(orderId, tagId);
    }

    @Override
    public List<OrderTag> listOrderTagByOrderIdSrcMaster(Long orderId, Long workId) {
        try (HintManager hintManager = HintManager.getInstance()) {
            hintManager.setMasterRouteOnly();
            return orderTagDao.listOrderTagByOrderId(orderId, workId);
        }
    }

    @Override
    public List<OrderTag> listOrderTagByOrderId(Long orderId, Long workId) {
        return orderTagDao.listOrderTagByOrderId(orderId, workId);
    }

    /**
     * 根据订单id集合查询标签列表
     *
     * @param orderIdList
     * @return
     */
    @Override
    public List<OrderTag> listOrderTagByOrderIdList(List<Long> orderIdList) {
        return orderTagDao.listOrderTagByOrderIdList(orderIdList);
    }

    @Override
    public Integer insertOrderTag(OrderTag orderTag) {
        return orderTagDao.insertOrderTag(orderTag);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    @OrderWorkChangeSync
    public void saveOrderTagList(OrderTagDTO orderTagDTO) {
        List<OrderTag> orderTagList = orderTagDTO.getOrderTagList();
        if (CollectionUtils.isEmpty(orderTagList)) {
            return ;
        }

        Date now = DateUtil.getNow();
        orderTagList.forEach(item -> {
            item.setOrderId(orderTagDTO.getOrderId());
            item.setWorkId(orderTagDTO.getWorkId());
            // 有效联系已设置标记时间
            if(Objects.isNull(item.getCreateTime())){
                item.setCreateTime(now);
            }
            item.setUpdateTime(now);
            //orderTagDao.insertOrderTag(item);
        });
        orderTagDao.insertOrderTagBatch(orderTagList);
    }

    @Override
    @OrderWorkChangeSync
    public Integer deleteOrderTagByTagIdList(OrderTagDelDTO orderTagDelDTO) {
        return orderTagDao.deleteOrderTagByTagIdList(orderTagDelDTO.getOrderId(), orderTagDelDTO.getWorkId(), orderTagDelDTO.getOrderTagIdList());
    }
}
